{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Vacancy in a Silicon Cluster\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "using JuLIP, JuLIP.Potentials, Optim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-8.669996510966682"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# reference energy\n",
    "#   energy per unit volume of a homogeneous silicon crystal \n",
    "at = bulk(\"Si\")\n",
    "calc = StillingerWeber()\n",
    "Eref = energy(calc, at) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-2210.8491102965504"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# cluster with vacancy\n",
    "at = bulk(\"Si\", cubic=true) * 4\n",
    "deleteat!(at, length(at) ÷ 2) \n",
    "set_calculator!(at, calc)\n",
    "# energy before relaxing\n",
    "E0 = energy(at)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Results of Optimization Algorithm\n",
      " * Algorithm: Conjugate Gradient\n",
      " * Starting Point: [0.0,0.0, ...]\n",
      " * Minimizer: [1.0275251355921737e-5,-4.670001322420145e-6, ...]\n",
      " * Minimum: -2.210849e+03\n",
      " * Iterations: 7\n",
      " * Convergence: true\n",
      "   * |x - x'| < 1.0e-32: false \n",
      "     |x - x'| = 4.22e-07 \n",
      "   * |f(x) - f(x')| / |f(x)| < 1.0e-32: false\n",
      "     |f(x) - f(x')| / |f(x)| = 1.32e-13 \n",
      "   * |g(x)| < 1.0e-05: true \n",
      "     |g(x)| = 9.58e-06 \n",
      "   * stopped by an increasing objective: false\n",
      "   * Reached Maximum Number of Iterations: false\n",
      " * Objective Calls: 15\n",
      " * Gradient Calls: 8\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "-2210.8491216209372"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# geometry optimisation\n",
    "set_constraint!(at, FixedCell(at))\n",
    "result = minimise!(at)\n",
    "E1 = result.minimum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vacancy Formation Energy without relaxing: 8.67\n",
      "Vacancy Formation Energy   with  relaxing: 8.67\n"
     ]
    }
   ],
   "source": [
    "# defect formation energy \n",
    "println(\"Vacancy Formation Energy without relaxing: \", round(E0 - (length(at)+1)*Eref/2, 4))\n",
    "println(\"Vacancy Formation Energy   with  relaxing: \", round(E1 - (length(at)+1)*Eref/2, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "ename": "LoadError",
     "evalue": "\u001b[91mUndefVarError: Visualise not defined\u001b[39m",
     "output_type": "error",
     "traceback": [
      "\u001b[91mUndefVarError: Visualise not defined\u001b[39m",
      "",
      "Stacktrace:",
      " [1] \u001b[1minclude_string\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::String, ::String\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./loading.jl:522\u001b[22m\u001b[22m"
     ]
    }
   ],
   "source": [
    "# visualise the configuration \n",
    "JuLIP.Visualise.display(at)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TODO Notes\n",
    "\n",
    "* urgently allow arbitrary boxes for Visualise\n",
    "* replace vacancy with interstitial (needs mechanism to add an atom)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.2-pre",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
